import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_menu/star_menu.dart';
import 'package:styled_widget/styled_widget.dart';

import '../../../utils/enum_utils.dart';
import '../controller/drawing_board_controller.dart';

/// 操作选项
class ActionsMenuWidget extends StatelessWidget {
  ActionsMenuWidget({
    super.key,
    this.onItemTapped,
  });

  /// 选项点击的回调事件
  void Function(int index, StarMenuController controller)? onItemTapped;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 30.r,
      height: 30.r,
      child: StarMenu(
        params: StarMenuParameters(
          shape: MenuShape.linear,
          linearShapeParams: LinearShapeParams(space: 12.r),
          centerOffset: Offset(0, 170.r),
        ),
        onItemTapped: onItemTapped,
        items: _actions(),
        child: Icon(Icons.menu, size: 30.r),
      ),
    );
  }

  List<Widget> _actions() {
    final dbc = Get.find<DrawingBoardController>();

    return [
      _getIcon(CupertinoIcons.arrow_turn_up_left).gestures(onTap: () {
        dbc.setActions(ActionsEnum.undo);
      }),
      _getIcon(CupertinoIcons.arrow_turn_up_right).gestures(onTap: () {
        dbc.setActions(ActionsEnum.redo);
      }),
      _getIcon(CupertinoIcons.rotate_right).gestures(onTap: () {
        dbc.setActions(ActionsEnum.turn);
      }),
      _getIcon(CupertinoIcons.trash).gestures(onTap: () {
        dbc.setActions(ActionsEnum.clear);
      }),
    ];
  }

  Widget _getIcon(IconData icon, {bool active = false}) {
    Color color = Colors.black87;
    if (active) {
      color = Colors.blue;
    }
    return Icon(icon, color: color, size: 30.r);
  }
}
